// pages/home/home.js
import {
    newslist
} from '../../utils/api'
Page({


    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        page: 1,
        pageSize: 5,
        openid: ''
    },
    yuyue() {
        wx.navigateTo({
            url: '../myreservation/myreservation?openid',
        })
    },

    sizes(){
        wx.navigateTo({
          url: '../size/size',
        })
    },
    async getlist() {
        let res = await newslist({
            page: this.data.page,
            pageSize: this.data.pageSize
        })
        // console.log(res)
        const {
            data
        } = res
        if (res.code == '00000') {
            const data = this.data.list.concat(res.data)
            // console.log(data)
            this.setData({
                list: data
            })
        }
    },
    mesubscribe() {
        wx.navigateTo({
            url: '/pages/minivisit/minivisit',
        })
    },
    subscribe() {
        wx.navigateTo({
          url: '/pages/visit/visit',
        })},
    // 跳转到校园动态详情
    xiangq(e){
        // console.log(1111);
        console.log(e);
        let id=e.currentTarget.dataset.id
        wx.navigateTo({
          url: '/pages/dynamic/dynamic?id='+id,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getlist()
        wx.getUserInfo({
            success: function (res) {
                // console.log(res)
                const userInfo = res.userInfo
                wx.login({
                    success(res) {
                        if (res.code) {
                            //发起网络请求
                            wx.request({
                                url: 'http://121.42.230.80/api/minivisit/login',
                                data: {
                                    code: res.code,
                                    userinfo: userInfo,
                                    appid: 'wxeb2f6528042b3bb6',
                                    secret: '992c635411e8835825c21576997c4baf'
                                },
                                method: "POST",
                                success(res) {
                                    console.log(res)
                                    if (res.data.code == '00000') {
                                        wx.setStorageSync('token', res.data.token)
                                        wx.setStorageSync('UserInfo', res.data.data)
                                    }
                                }
                            })
                        } else {
                            console.log('登录失败！' + res.errMsg)
                        }
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.data.page = 1
        this.data.list = []
        this.getlist()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.data.page++
        this.getlist()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})